﻿using ChinaCircleJerk.Adapter;
using ChinaCircleJerk.Adapter.Data;
using ChinaCircleJerk.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;

namespace ChinaCircleJerk.Controllers
{
    [RequireHttps]
    public class HomeController : Controller
    {
        private CCJerkAdapter _adapter;
        public HomeController()
        {
            _adapter = new CCJerkDataAdapter();
        }
        public HomeController(CCJerkAdapter Adapter)
        {
            _adapter = Adapter;
        }
        public ActionResult Index()
        {
            var uId = User.Identity.GetUserId();
            IndexVm iVm = _adapter.GetPosts();
            iVm.UserId = uId;
            return View(iVm);
        }
        [Authorize]
        [HttpGet]
        public ActionResult Create()
        {
            PostVm pVm = new PostVm();
            return View(pVm);
        }
        [HttpPost]
        public ActionResult Create(PostVm pVm)
        {
            var uId = User.Identity.GetUserId();
            pVm.UserId = uId;
            _adapter.CreatePost(pVm);
            return RedirectToAction("Index");
        }
        [Authorize]
        [HttpGet]
        public ActionResult CreateReply(int id)
        {
            ReplyVm rVm = new ReplyVm();
            rVm.PostId = id;
            return View(rVm);
        }
        [HttpPost]
        public ActionResult CreateReply(ReplyVm rVm)
        {
            var uId = User.Identity.GetUserId();
            rVm.UserId = uId;
            _adapter.CreateReply(rVm);
            return RedirectToAction("Details/" + rVm.PostId);
        }
        [Authorize]
        public ActionResult Details(int id)
        {
            DetailsVm dVm = _adapter.PostDetails(id);
            return View(dVm);
        }
        [Authorize]
        [HttpGet]
        public ActionResult Edit(int id)
        {
            EditVm eVm = _adapter.EditPost(id);
            return View(eVm);
        }
        [HttpPost]
        public ActionResult Edit(EditVm eVm)
        {
            _adapter.UpdatePost(eVm);
            return RedirectToAction("Index");
        }
        [Authorize]
        public ActionResult DeletePost(int id)
        {
            _adapter.DeletePost(id);
            return RedirectToAction("Index");
        }
        [Authorize]
        public ActionResult DeleteReply(int id)
        {
            _adapter.DeleteReply(id);
            return RedirectToAction("Index");
        }


        /* ---------------------------------------------------------------------------------- */
        [Authorize]
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }
        [Authorize]
        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}